package com.ust.diamond;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import libsvm.svm_model;

public class Chess {
	public static int size = 8;
	private int diamond[][] = new int[size][size];
	
	public Chess(BufferedImage img, svm_model diamondModel) throws IOException {
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				Diamond diamond = new Diamond(i, j, img, diamondModel);
				this.diamond[i][j] = diamond.color;
			}
		}
//		System.out.println("(0,5) is " + new Diamond(0, 5, img).getCharacter());
//		System.out.println("(1,5) is " + new Diamond(1, 5, img).getCharacter());
//		System.out.println("(4,5) is " + new Diamond(4, 5, img).getCharacter());
//		System.out.println("(6,5) is " + new Diamond(6, 5, img).getCharacter());
//		System.out.println("(6,6) is " + new Diamond(6, 6, img).getCharacter());
//		this.print();
	}
	
	public Chess(Chess chess) {
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				this.diamond[i][j] = chess.diamond[i][j];
			}
		}
		
	}
	
	public Point[] lookForClick() {
		Point point1 = new Point(), point2 = new Point();
		int index[] = {-1, 0, 1};
		int i2, j2;
		int i_start, j_start;
		Random rand = new Random();
		i_start = rand.nextInt(size);
		j_start = rand.nextInt(size);
		for(int i = i_start; i >=0; i--) {
			for(int j = j_start; j >=0; j--) {
				point1 = new Point(i, j);
				for(int u = 0; u <3; u++) {
					for(int v = 0; v<3; v++) {
						if(u == v) continue;
						if(u != 1 && v != 1) continue;
						i2 = i+index[u];
						j2 = j+index[v];
						if(i2 < 0 || i2 >= size || j2 < 0 || j2 >= size) {
							continue;
						}
						point2 = new Point(i2, j2);

						if(thereIsBingo(exchange(point1, point2))) {
							return new Point[]{point1, point2};
						}
						
					}
				}
				
			}
		}
		// It is possible that no bingo has been found.
		// That situation needs to be handled here.
		return new Point[]{point1, point2};
		
	}
	
	/**
	 * 
	 * @param point1 src
	 * @param point2 des
	 * @return
	 */
	private Chess exchange(Point point1, Point point2) {
		Chess chess = new Chess(this);		
		int color = chess.getColor(point1.x, point1.y);
		chess.setColor(point1.x, point1.y, chess.getColor(point2.x, point2.y));
		chess.setColor(point2.x, point2.y, color);
//		if(point1.x == 4 && point1.y == 5 && point2.x == 4 && point2.y == 6) {
//			chess.print();
//		}
//		System.out.println();
//		chess.print();
		return chess;
	}
	
	public int getColor(int x, int y) {
		return diamond[x][y];
	}
	
	public void setColor(int x, int y, int color) {
		diamond[x][y] = color;
	}
	
	// This method can be improved.
	public static boolean thereIsBingo(Chess chess) {
		for(int i = 0; i < size; i++) {
			for(int j = 0; j < size; j++) {
				int baseColor = chess.getColor(i, j);
//				if(i == 4 && j==6) {
//					int d;
//				}
				if((i+2 < size && 
						chess.getColor(i+1, j) == baseColor &&
						chess.getColor(i+2, j) == baseColor) ||
						(j+2 < size && 
						chess.getColor(i, j+1) == baseColor &&
						chess.getColor(i, j+2) == baseColor)) {
					if(SolveIt.debugMode)
						chess.print();
					return true;
				}
				
			}
		}
		return false;
	}
	
	public void print() {
		for(int i = 0; i < 8; i++) {
			for(int j = 0; j < 8; j++) {
				System.out.print(diamond[i][j] + " ");
			}
			System.out.println();
		}
	}

}
